package com.xiaotu.common.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Glyph {
	
	private String id;   //字体加密前的原文，例如1
	private String name; //字体加密后的密文，例如uniE24E
    private String xMin;  //x轴最小值
    private String yMin;  //y轴最小值
    private String xMax;  //x轴最大值 
    private String yMax;  //y轴最大值      
    private List<Contour> contourList;  //轮廓信息列表
    private List<Glyph> glyphList;
    private int matchValue;//匹配值
    
    public  Glyph() {
	}   
    
    public  Glyph(String id,String name,String xMin,String yMin,String xMax,String yMax) {
    	this.id = id;
    	this.name = name;
		this.xMin = xMin;
		this.yMin = yMin;
		this.xMax = xMax;
		this.yMax = yMax;
	}   

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getxMin() {
		return xMin;
	}
	public void setxMin(String xMin) {
		this.xMin = xMin;
	}
	public String getyMin() {
		return yMin;
	}
	public void setyMin(String yMin) {
		this.yMin = yMin;
	}
	public String getxMax() {
		return xMax;
	}
	public void setxMax(String xMax) {
		this.xMax = xMax;
	}
	public String getyMax() {
		return yMax;
	}
	public void setyMax(String yMax) {
		this.yMax = yMax;
	}
	public List<Contour> getContourList() {
		return contourList;
	}
	public void setContourList(List<Contour> contourList) {
		this.contourList = contourList;
	}

	public List<Glyph> getGlyphList() {
		return glyphList;
	}

	public void setGlyphList(List<Glyph> glyphList) {
		this.glyphList = glyphList;
	}

	public int getMatchValue() {
		return matchValue;
	}

	public void setMatchValue(int matchValue) {
		this.matchValue = matchValue;
	}

	@Override
    public boolean equals(Object obj) {
        if(obj==null){
            return false;
        }
        if(this==obj){
            return true;
        }
        if(obj instanceof Glyph){
        	Glyph glyph=(Glyph) obj;
            if(glyph.xMin.equals(this.xMin)
            		&&glyph.yMin.equals(this.yMin)
            		&&glyph.xMax.equals(this.xMax)
            		&&glyph.yMax.equals(this.yMax)){
            	List<Contour> contourList = glyph.getContourList();
            	if(contourList==null && this.contourList==null) {
            		return true;
            	}

            	if(contourList.size() == this.contourList.size()) {
            		for(int i=0;i<contourList.size();i++) {
            			Contour contour = contourList.get(i);
            			Contour thisContour = this.contourList.get(i);
            			if(!contour.equals(thisContour)) {
            				return false;
            			}           			
            		}
            		return true;
            	}else {
            		return false;
            	}
            }else{
                return false;
            }
        }
        return false;
    }
    
	public Glyph match(List<Glyph> setGlyphList) {
		
        if(null == setGlyphList || setGlyphList.isEmpty()){
            return null;
        }
        
        List<Glyph> matchGlyphList = new ArrayList<Glyph>();
        
        for(Glyph glyph : setGlyphList){
        	if(this.getContourList().size() == glyph.getContourList().size()){
        		int matchValue = 0;
        		for(int i =0;i<this.getContourList().size();i++){
        			Contour ct_this = this.getContourList().get(i);
        			Contour ct_template = glyph.getContourList().get(i);
        			//匹配pt标签的长度偏差值最大为5
        			if(Math.abs((ct_this.getPointList().size())-(ct_template.getPointList().size()))<=10){
        				for(Point pt_this : ct_this.getPointList()){
            				for(Point pt_template : ct_template.getPointList()){
            					if(pt_this.equals(pt_template)){
            						matchValue++;
            					}
            				}
                		}
        			}else{
        				break;
        			}
        		}
        		glyph.setMatchValue(matchValue);
        		if(matchValue>0){
        			matchGlyphList.add(glyph);
        		}
        	}
        }
        
        if(matchGlyphList.size()==0 || matchGlyphList.isEmpty()){
        	return null;
        }
        
        if(matchGlyphList.size()>1){
        	
        	Collections.sort(matchGlyphList,new Comparator<Glyph>() {
            	
    			@Override
    			public int compare(Glyph o1, Glyph o2) {
    				
    				if(o1.getMatchValue()-o2.getMatchValue()>0){
    					return 1;
    				}else if(o1.getMatchValue()-o2.getMatchValue()<0){
    					return -1;
    				}else{
    					return 0;
    				}
    			}
            	
    		});
        	Collections.reverse(matchGlyphList);
        }
        
        return matchGlyphList.get(0);
    }
	
}